package models

type CliSetting struct {
	Id    int    `xorm:"int pk autoincr"`
	Code  string `xorm:"varchar(32) notnull"`
	Key   string `xorm:"varchar(64) notnull"`
	Value string `xorm:"varchar(4096) notnull default '' "`
}

//type Slack struct {
//	Url      string    `json:"url"`
//	Channels []Channel `json:"channels"`
//	Template string    `json:"template"`
//}

func (setting *CliSetting) KeyCodeExists(code, key string) (bool, error) {
	//if id == 0 {
	//	count, err := Db.Where("name = ?", name).Count(host)
	//	return count > 0, err
	//}

	count, err := Db.Where("Code = ? AND Key = ?", code, key).Count(setting)
	return count > 0, err
}

// 初始化基本字段 邮件、slack等
func (setting *CliSetting) InitBasicField() {


	isExist, err := setting.KeyCodeExists(userCode, userCodeKey)//set表记录密码里的账号;点了记录密码账号密码存在客户端user表
	if err == nil {
		if isExist == false {
			setting.CreateStorePath("")
		}

	}
	setting.Id = 0

	isExist, err = setting.KeyCodeExists(IPCode, IPCodeKey)//set表记录密码里的账号;点了记录密码账号密码存在客户端user表
	if err == nil {
		if isExist == false {
			setting.CreateIPServPath("-1")
		}

	}
	setting.Id = 0


}

const (
	userCode    = "usr"
	userCodeKey = "una" //name

	IPCode    = "IP"
	IPCodeKey = "IPServ" //name

)

func (setting *CliSetting) StorePath() (string, error) {
	list := make([]CliSetting, 0)
	err := Db.Where("code = ?", userCode).Find(&list)
	//slack := Slack{}
	if err != nil {
		return "", err
	}

	var str string
	setting.formatStorePath(list, &str)

	return str, err
}

func (setting *CliSetting) formatStorePath(list []CliSetting, str *string) {
	for _, v := range list {
		switch v.Key {
		case userCodeKey:
			*str = v.Value
			//case SlackTemplateKey:
			//	slack.Template = v.Value
			//default:
			//	slack.Channels = append(slack.Channels, Channel{
			//		v.Id, v.Value,
			//	})
		}
	}
}

func (setting *CliSetting) UpdateStorePath(path string) error { //url,
	//setting.Value = url
	//
	//Db.Cols("value").Update(setting, Setting{Code: SlackCode, Key: SlackUrlKey})

	setting.Value = path
	_, err := Db.Cols("value").Update(setting, Setting{Code: userCode, Key: userCodeKey})

	return err
}

func (setting *CliSetting) CreateStorePath(path string) (int64, error) {
	setting.Code = userCode
	setting.Key = userCodeKey
	setting.Value = path

	return Db.Insert(setting)
}

func (setting *CliSetting) IsChannelExist(channel string) bool {
	setting.Code = SlackCode
	setting.Key = SlackChannelKey
	setting.Value = channel

	count, _ := Db.Count(setting)

	return count > 0
}

func (setting *CliSetting) IPServPath() (string, error) {
	list := make([]CliSetting, 0)
	err := Db.Where("code = ?", IPCode).Find(&list)
	//slack := Slack{}
	if err != nil {
		return "", err
	}

	var str string
	setting.formatIPServPath(list, &str)

	return str, err
}

func (setting *CliSetting) formatIPServPath(list []CliSetting, str *string) {
	for _, v := range list {
		switch v.Key {
		case IPCodeKey:
			*str = v.Value
			//case SlackTemplateKey:
			//	slack.Template = v.Value
			//default:
			//	slack.Channels = append(slack.Channels, Channel{
			//		v.Id, v.Value,
			//	})
		}
	}
}

func (setting *CliSetting) UpdateIPServPath(path string) error { //url,
	//setting.Value = url
	//
	//Db.Cols("value").Update(setting, Setting{Code: SlackCode, Key: SlackUrlKey})

	setting.Value = path
	_, err := Db.Cols("value").Update(setting, Setting{Code: IPCode, Key: IPCodeKey})

	return err
}

func (setting *CliSetting) CreateIPServPath(path string) (int64, error) {
	setting.Code = IPCode
	setting.Key = IPCodeKey
	setting.Value = path

	return Db.Insert(setting)
}


//type Mail struct {
//	Host      string     `json:"host"`
//	Port      int        `json:"port"`
//	User      string     `json:"user"`
//	Password  string     `json:"password"`
//	MailUsers []MailUser `json:"mail_users"`
//	Template  string     `json:"template"`
//}

//type MailUser struct {
//	Id       int    `json:"id"`
//	Username string `json:"username"`
//	Email    string `json:"email"`
//}

//const (
//	compactTp7Z = iota
//	compactTpZIP
//)
//
//const (
//	cptLvOnlyStore = iota
//	cptLvMostSpd
//	cptLvMoreSpd
//	cptLvStandSpd
//	cptLvMoreGood
//	cptLvMostGood
//)
//
//const (
//	cptMothedLZMA = iota
//	cptMothedLZMA2
//)
//
//const (
//	ENCMothedNone = iota
//	ENCMothedGeneral
//	ENCMothedAES128
//	ENCMothedAES256
//)
//
//type Compact struct {
//	CptType   int32    `json:"cpttype"`   //cpttype
//	CptLevel  int32    `json:"cptlevel"`  //compactLevel
//	Cptmethod int32    `json:"cptmethod"` //cptmethod
//	Encmethod int32    `json:"encmethod"`
//	Pasd      string `json:"pasd"`
//	//DBType  int32
//}
//
//const (
//	CompactCode    = "compact"
//	CompactCodeKey = "compact"
//)
//
//// region 邮件配置
//func (setting *CliSetting) Compact() (Compact, error) {
//	list := make([]CliSetting, 0)
//	err := Db.Where("code = ?", CompactCode).Find(&list)
//	compact := Compact{} //MailUsers: make([]MailUser, 0)
//	if err != nil {
//		return compact, err
//	}
//
//	setting.formatCompact(list, &compact)
//
//	return compact, err
//}
//
//func (setting *CliSetting) formatCompact(list []CliSetting, compact *Compact) {
//	for _, v := range list {
//		switch v.Key {
//		case CompactCodeKey:
//			json.Unmarshal([]byte(v.Value), compact)
//			//case MailUserKey:
//			//	json.Unmarshal([]byte(v.Value), &mailUser)
//			//	mailUser.Id = v.Id
//			//	mail.MailUsers = append(mail.MailUsers, mailUser)
//			//case MailTemplateKey:
//			//	mail.Template = v.Value
//		}
//
//	}
//}
//
//func (setting *CliSetting) UpdateCompact(config string) error { //, template string
//	setting.Value = config
//	_,err := Db.Cols("value").Update(setting, Setting{Code: CompactCode, Key: CompactCodeKey})
//
//	//setting.Value = template
//	//Db.Cols("value").Update(setting, Setting{Code: MailCode, Key: MailTemplateKey})
//
//	return err
//}
//
//
//func (setting *CliSetting) CreateCompact(cptType, cptLevel,cptmethod, encmethod int32, pasd string) (int64, error) {
//	setting.Code = CompactCode
//	setting.Key = CompactCodeKey
//	//mailUser := MailUser{0, username, email}
//	compact := Compact{cptType, cptLevel, cptmethod, encmethod,pasd}
//	jsonByte, err := json.Marshal(compact)
//	if err != nil {
//		return 0, err
//	}
//	setting.Value = string(jsonByte)
//
//	return Db.Insert(setting)
//}
//
//func (setting *CliSetting) RemoveCompact(id int) (int64, error) {
//	setting.Code = CompactCode
//	setting.Key = CompactCodeKey
//	setting.Id = id
//	return Db.Delete(setting)
//}
